package com.neouton.crm.dictionary.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.dictionary.entity.DictionaryLookupItemVO;
import com.neouton.crm.dictionary.entity.DictionaryLookupVO;
import com.neouton.crm.dictionary.entity.SysMenuVO;
import com.neouton.crm.dictionary.service.IDictionaryService;
import com.neouton.crm.entity.SysUserEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("dictionary")
public class DictionaryController {

    @Resource
    private IDictionaryService dictionaryService;

    @PostMapping("queryDictionarysByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryDictionarysByPage(@PathVariable(value = "pageNum")Integer pageNum,
                                           @PathVariable(value = "pageSize")Integer pageSize,
                                           @RequestBody DictionaryLookupVO dataDictionaryLookup){
        IPage<DictionaryLookupVO> page = new Page<>(pageNum, pageSize, true);
        IPage<DictionaryLookupVO> dataDictionaryList = dictionaryService.queryDictionarysByPage(page,dataDictionaryLookup);
        return ResponseAPI.successData(new PageUtil(dataDictionaryList));
    }

    /**
     * 数据字典查询详情
     * 对外提供接口，供其它系统使用
     *
     * @param classfiedCode 字典名称
     */
    @PostMapping("queryCrmDictionaryLookupInfo")
    public ResponseAPI queryDictionaryLookupInfo(@RequestBody List<String> classfiedCode){
        Map<String, List<DictionaryLookupItemVO>> dataDictionaryLookupItems = dictionaryService.queryDictionaryLookupInfo(null,classfiedCode);
        return ResponseAPI.successData(dataDictionaryLookupItems);
    }

    /**
     * 数据字典详情新增
     *
     * @param dictionaryLookupItems
     * @return
     */
    @PostMapping("addDictionaryLookupItem/{id}")
    public ResponseAPI addDictionaryLookupItem(@PathVariable(value = "id") Integer id, @RequestBody List<DictionaryLookupItemVO> dictionaryLookupItems){
        Boolean flage = dictionaryService.addDictionaryLookupItem(id,dictionaryLookupItems);
        return  ResponseAPI.successData("success") ;

    }

    /**
     * 数据字典查询详情
     * 对外提供接口，供其它系统使用
     *
     * @param classfiedCode 字典名称
     */
    @PostMapping("queryCrmDictionaryLookupInfo/{status}")
    public ResponseAPI queryDictionaryLookupInfo(@PathVariable(value = "status") Integer status,
                                                 @RequestBody List<String> classfiedCode){
        Map<String, List<DictionaryLookupItemVO>> dataDictionaryLookupItems = dictionaryService.queryDictionaryLookupInfo(status,classfiedCode);
        return ResponseAPI.successData(dataDictionaryLookupItems);
    }


    /**
     * 获取各模块管理员
     * @param pageNum
     * @param pageSize
     * @param sysMenuVO
     * @return
     */
    @PostMapping("queryCrmAdminByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryCrmAdminByPage(@PathVariable(value = "pageNum")Integer pageNum,
                                              @PathVariable(value = "pageSize")Integer pageSize,
                                              @RequestBody SysMenuVO sysMenuVO){
        IPage<SysMenuVO> page = new Page<>(pageNum, pageSize, true);
        IPage<SysMenuVO> dataDictionaryList = dictionaryService.queryCrmAdminByPage(page,sysMenuVO);
        return ResponseAPI.successData(new PageUtil(dataDictionaryList));
    }

    /**
     * 获取管理员集合
     * @param menuCode
     * @return
     */
    @PostMapping("queryMenuUserIdsByCode/{menuCode}")
    public ResponseAPI queryMenuUserIdsByCode(@PathVariable(value = "menuCode")String menuCode){
        List<SysUserEntity> userEntities = dictionaryService.queryMenuUserIdsByCode(menuCode);
        return ResponseAPI.successData(userEntities);
    }

    /**
     * 保存管理员信息
     * @param menuId
     * @return
     */
    @PostMapping("saveMenuUserIds/{menuId}")
    public ResponseAPI saveMenuUserIds(@PathVariable(value = "menuId")Integer menuId,
                                       @RequestBody List<SysUserEntity> users){
        Boolean flage = dictionaryService.saveMenuUserIds(menuId,users);
        return ResponseAPI.successData(flage);
    }

    /**
     * 查询是否为管理员
     * @return
     */
    @PostMapping("queryIsMenuUserByUserId")
    public ResponseAPI queryIsMenuUserByUserId(){
        Boolean flage = dictionaryService.queryIsMenuUserByUserId();
        return ResponseAPI.successData(flage);
    }

    /**
     * 获取抄送人列表
     * @param pageNum
     * @param pageSize
     * @param sysMenuVO
     * @return
     */
    @PostMapping("queryCrmCcByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryCrmCcByPage(@PathVariable(value = "pageNum")Integer pageNum,
                                        @PathVariable(value = "pageSize")Integer pageSize,
                                        @RequestBody SysMenuVO sysMenuVO,
                                        @RequestHeader("token") String token){
        IPage<SysMenuVO> page = new Page<>(pageNum, pageSize, true);
        IPage<SysMenuVO> dataDictionaryList = dictionaryService.queryCrmCcByPage(page,sysMenuVO,token);
        return ResponseAPI.successData(new PageUtil(dataDictionaryList));
    }

    /**
     * 获取抄送人集合
     * @param eventCode
     * @return
     */
    @PostMapping("queryCcUserIdsByCode/{eventCode}")
    public ResponseAPI queryCcUserIdsByCode(@PathVariable(value = "eventCode")String eventCode){
        List<SysUserEntity> userEntities = dictionaryService.queryCcUserIdsByCode(eventCode);
        return ResponseAPI.successData(userEntities);
    }

    /**
     * 保存抄送人信息
     * @param eventId
     * @return
     */
    @PostMapping("saveCcUserIds/{eventId}")
    public ResponseAPI saveCcUserIds(@PathVariable(value = "eventId")Integer eventId,
                                       @RequestBody List<SysUserEntity> users){
        Boolean flage = dictionaryService.saveCcUserIds(eventId,users);
        return ResponseAPI.successData(flage);
    }

    /**
     * 获取权限管理列表
     * @param pageNum
     * @param pageSize
     * @param sysMenuVO
     * @return
     */
    @PostMapping("queryCrmAuthByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryCrmAuthByPage(@PathVariable(value = "pageNum")Integer pageNum,
                                        @PathVariable(value = "pageSize")Integer pageSize,
                                        @RequestBody SysMenuVO sysMenuVO){
        IPage<SysMenuVO> page = new Page<>(pageNum, pageSize, true);
        IPage<SysMenuVO> dataDictionaryList = dictionaryService.queryCrmAuthByPage(page,sysMenuVO);
        return ResponseAPI.successData(new PageUtil(dataDictionaryList));
    }

    /**
     * 获取权限管理人集合
     * @param authCode
     * @return
     */
    @PostMapping("queryAuthUserIdsByCode/{authCode}")
    public ResponseAPI queryAuthUserIdsByCode(@PathVariable(value = "authCode")String authCode){
        List<SysUserEntity> userEntities = dictionaryService.queryAuthUserIdsByCode(authCode);
        return ResponseAPI.successData(userEntities);
    }

    /**
     * 保存抄送人信息
     * @param authManageId
     * @return
     */
    @PostMapping("saveAuthUserIds/{authManageId}")
    public ResponseAPI saveAuthUserIds(@PathVariable(value = "authManageId")Integer authManageId,
                                     @RequestBody List<SysUserEntity> users){
        Boolean flage = dictionaryService.saveAuthUserIds(authManageId,users);
        return ResponseAPI.successData(flage);
    }
}
